
var express = require('express');
var router = express.Router();
var sqlQuery = require('../../../module/sql');
var jiami = require('../../../module/jiami');



/* GET users listing. */
router.get('/',async function (req, res, next) {
  //获取用户id
  let userid = req.query.id;
  let sqlStr = "select * from user where id = ?";
  let result = await sqlQuery(sqlStr,[userid])
  let user = result[0]
  //通过角色表获取所有角色
  let role = await getRole()
  let options = {user,role};

  res.render('admin/user/edit',options);

});


//获取用户角色
async function getRole(){
  let sqlStr = 'select * from role'
  let result = await sqlQuery(sqlStr);
  //结果返回
  return Array.from(result);
}


//获取修改的用户信息
router.post('/',async(req,res)=>{
  let email = req.body.email
  let phone = req.body.phone
  let roleid = req.body.roleid
  let username = req.body.username
  let state = req.body.state
  console.log("state:",state)
  if(state == 'on'){
    state = 1
  }else{
    state = 0
  }
  console.log("state:",state)
  //更新数据
  let sqlStr = "update user set email=?, phone=?, roleid=?, state=? where username=? ";
  let arr = [email,phone,roleid,state,username]
  await sqlQuery(sqlStr,arr);
      res.json({
        state:"ok",
        content:"用户信息修改成功"
      })
})


module.exports = router;
